<?php

namespace app\zmt\controller;

use cmf\controller\AdminBaseController;
use app\zmt\model\BannerModel;
use app\zmt\validate\BannerValidate;

class BannerController extends AdminBaseController
{
    public function index()
    {
        $keyword = input('get.keyword', '');
        $where = [];
        if ($keyword) {
            $where[] = ['id', 'like', '%' . $keyword . '%'];
        }

        $bannerList = BannerModel::where($where)
            ->order('list_order', 'desc')
            ->paginate(5);

        $page = $bannerList->render();
        $this->assign('bannerList', $bannerList);
        $this->assign('page', $page);
        return $this->fetch();
    }

    public function add()
    {
        return $this->fetch('add');
    }

    public function addPost()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();

            $validate = new BannerValidate();
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }

            $banner = new BannerModel();
            $banner->url = $data['url'];
            $banner->image = $data['image'];
            $banner->switch = isset($data['switch']) ? $data['switch'] : 1;
            $banner->create_time = date('Y-m-d H:i:s');

            if ($banner->save()) {
                $this->success('添加成功', url('Banner/index'));
            } else {
                $this->error('添加失败');
            }
        }
    }

    public function changeStatus()
    {
        $id = input('param.id', 0, 'intval'); // 获取传入的新闻 ID
        $news = BannerModel::find($id);

        if ($news) {
            // 切换状态
            $news->switch = $news->switch == 1 ? 0 : 1;

            if ($news->save()) {
                $this->success('状态切换成功', 'Banner/index'); // 成功后重定向到新闻列表页
            } else {
                $this->error('状态切换失败', 'Banner/index'); // 失败后也重定向到新闻列表页
            }
        } else {
            $this->error('新闻不存在', 'Banner/index'); // 新闻不存在时重定向到新闻列表页
        }
    }

    public function edit($id)
    {
        $banner = BannerModel::find($id);
        if (!$banner) {
            $this->error('轮播图不存在');
        }

        $this->assign('banner', $banner);
        return $this->fetch('banner/edit');
    }


    public function editPost()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();

            $validate = new BannerValidate();
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }

            $banner = BannerModel::find($data['id']);
            if (!$banner) {
                $this->error('轮播图不存在');
            }

            $banner->url = $data['url'];
            $banner->image = $data['image'];
            $banner->switch = isset($data['switch']) ? $data['switch'] : 1;
            $banner->update_time = date('Y-m-d H:i:s');

            if ($banner->save()) {
                $this->success('编辑成功', url('Banner/index'));
            } else {
                $this->error('编辑失败');
            }
        }
    }

    public function delete($id)
    {
        $banner = BannerModel::find($id);
        if (!$banner) {
            $this->error('轮播图不存在');
        }

        if (BannerModel::destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    public function order()
    {
        parent::listOrders(new BannerModel());
        $this->success('排序成功');
    }
}
